import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mapbox_maps_flutter/mapbox_maps_flutter.dart';

import '../../../contract.dart';
import '../manager/draw_type.dart';
import '../manager/move_type.dart';

/// 地块widget 类，用于绘制地块的形状
class LandWidget extends StatefulWidget {
  DrawType? drawType;
  int? selectPosition;
  List<ScreenCoordinate>? position;
  MoveType? moveType;
  Offset? moveOffset;

  LandWidget(
      {Key? key,
      this.drawType,
      this.selectPosition,
      this.position,
      this.moveType,
      this.moveOffset})
      : super(key: key);

  @override
  State<LandWidget> createState() => _LandWidgetState();
}

class _LandWidgetState extends State<LandWidget> {
  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: _LandPainter(widget),
      size: const ui.Size(double.infinity, double.infinity),
    );
  }
}

class _LandPainter extends CustomPainter {
  late Paint _pointPaint;
  late Paint _selectPointPaint;
  late Paint _linePaint;
  late Paint _polygonPaint;
  LandWidget widget;
  final Path _path = Path();

  _LandPainter(this.widget) {
    _pointPaint = Paint()
      ..color = Contract.LAND_POINT_COLOR
      ..isAntiAlias = true;
    _selectPointPaint = Paint()
      ..color = const Color(0xFFFF0000)
      ..isAntiAlias = true;
    _linePaint = Paint()
      ..color = Contract.LAND_LINE_COLOR
      ..style = PaintingStyle.stroke
      ..strokeWidth = Contract.LAND_LINE_WIDTH.w
      ..isAntiAlias = true;
    _polygonPaint = Paint()
      ..style = PaintingStyle.fill
      ..color = Contract.LAND_POLYGON_COLOR
          .withAlpha((Contract.LAND_POLYGON_OPACITY * 255).toInt())
      ..isAntiAlias = true;
  }

  @override
  void paint(Canvas canvas, ui.Size size) {
    if (widget.position == null ||
        widget.drawType == null ||
        widget.selectPosition == null ||
        widget.moveType == null) {
      return;
    }
    if (widget.position!.isEmpty) {
      return;
    }

    if (widget.moveType! == MoveType.END_MOVE) {
      return;
    }

    if (widget.moveType! == MoveType.MOVING) {
      if (widget.moveOffset == null) {
        return;
      }
      var offset = widget.moveOffset!;
      widget.position![widget.selectPosition! - 1] =
          ScreenCoordinate(x: offset.dx, y: offset.dy);
    }
    _drawLine(canvas);
    _drawPoint(canvas);
  }

  void _drawLine(Canvas canvas) {
    _path.reset();
    var list = widget.position!;
    if (list.length < 2) {
      return;
    }
    for (var i = 0; i < list.length; i++) {
      ScreenCoordinate coordinate = list[i];
      if (i == 0) {
        _path.moveTo(coordinate.x, coordinate.y);
      } else {
        _path.lineTo(coordinate.x, coordinate.y);
      }
    }

    switch (widget.drawType!) {
      case DrawType.DRAW_LAND:
      case DrawType.MEASURE_AREA:
        _path.close();
        if (list.length > 2) {
          canvas.drawPath(_path, _polygonPaint);
        }
        break;
      case DrawType.MEASURE_LENGTH:
        break;
    }
    canvas.drawPath(_path, _linePaint);
  }

  void _drawPoint(Canvas canvas) {
    var list = widget.position!;
    for (var value in list) {
      canvas.drawCircle(
          Offset(value.x, value.y), Contract.LAND_POINT_WIDTH.w, _pointPaint);
    }
    var target = list[widget.selectPosition! - 1];
    canvas.drawCircle(Offset(target.x, target.y), 8.w, _selectPointPaint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
